// 输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
// 输出: [["bat"],["nat","tan"],["ate","eat","tea"]]

/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function (strs) {
  const m = new Map()
  strs.forEach(s => {
    const key = [...s].sort().join('')
    if (m.has(key)) {
      m.get(key).push(s)
    } else {
      m.set(key, [s])
    }
  })

  return Array.from(m.values())
}

console.log(
  groupAnagrams(
    ["eat", "tea", "tan", "ate", "nat", "bat"]
  )
)
